package com.framework.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * <p>Title:用来转换时间的工具类</p>
 * <p>Description:</p>
 * @author www.51wuxia.com
 * @version 1.0
 */
public class DateUtil {
	/**
	 * 时间转换方法
	 * @param date 传入一个String的时间
	 * @return 获得年月日的时间(yyyy-MM-dd)
	 */
	public static String formatDate(Date date){		
		//保存改后的时间
		String ymd=" ";
		if(date==null||"".equals(date)||"1900-01-01 00:00:00.0".equals(date)){
			return ymd;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		ymd=dateFormat.format(date);
		return ymd;
	}
	/**
	 * 
	 * @return 当前时间年
	 */
	public static String getYear() {
		return getCurrentDateTime("yyyy");
	}

	/**
	 * 获得给定时间的月分的会后一天
	 * @param calendar
	 * @return 天数
	 */
	public int monthLastDay(GregorianCalendar calendar) {
		
		int lastDay = 0;
		GregorianCalendar gregorian = calendar;		
		int month = gregorian.get(Calendar.MONTH);		
		do {
			lastDay = gregorian.get(Calendar.DAY_OF_MONTH);
			gregorian.add(Calendar.DAY_OF_MONTH, 1);
		} while (gregorian.get(Calendar.MONTH) == month);	
		return lastDay;
	}
	
	/**
	 * 返回这个月的最大Date
	 * @return YYYY-MM-dd
	 */
	public String getMonthLastDaDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		datetime.append(monthLastDay(calendar));
		return datetime.toString();
	}
	/**
	 * 返回这个月的最小Date
	 * @return YYYY-MM-dd
	 */
	public String getMonthLeastDaDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		datetime.append("01");
		return datetime.toString();
	}
	
	/**
	 * 获得当前日期YYYY-MM-dd
	 * @return YYYY-MM-dd
	 */
	public String getFormatDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("-");
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("-");
		if(calendar.get(Calendar.DAY_OF_MONTH)<10){
			datetime.append("0");
		}
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		return datetime.toString();
	}
	
	/**
	 * 获得 XXXX年XX月XX日XX分XX秒 格式的的时间
	 * @return
	 */
	public String getFormatTime(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		datetime.append("年");
		datetime.append((calendar.get(Calendar.MONTH)+1));
		datetime.append("月");	
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		datetime.append("日");
		datetime.append(calendar.get(Calendar.MINUTE));
		datetime.append("分");
		datetime.append(calendar.get(Calendar.SECOND));
		datetime.append("秒");
		return datetime.toString();
	}
	
	/**
	 * 获得 当前日期YYYYMMdd
	 * @return
	 */
	public static  String getDate(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.YEAR));
		if(calendar.get(Calendar.MONTH)<9){		
			datetime.append("0");
		}
		datetime.append((calendar.get(Calendar.MONTH)+1));
		if(calendar.get(Calendar.DAY_OF_MONTH)<10){
			datetime.append("0");
		}
		datetime.append(calendar.get(Calendar.DAY_OF_MONTH));
		return datetime.toString();
	}
	
	
	/**
	 *  获得当前时间 HH:MM
	 * @return
	 */
	public String getTime(){
		StringBuffer datetime=new StringBuffer();
		GregorianCalendar calendar= new GregorianCalendar();	
		datetime.append(calendar.get(Calendar.HOUR_OF_DAY));
		datetime.append(":");
		datetime.append(calendar.get(Calendar.MINUTE));
		return datetime.toString();
	}
	
	/**
	 *  获得当前时间 HHmmss
	 * @return
	 */
	public static String getFullTimeNotDate(){
		return getCurrentDateTime("HHmmss");
	}
	/**
	 *  获得当前时间 HHmmss
	 * @return
	 */
	public static String getFullTimeByTwoPonit(){
		return getCurrentDateTime("HH:mm:ss");
	}
	/**
	 *  获得当前时间 yyyyMMdd
	 * @return
	 */
	public static String getFullDate(){
		return getCurrentDateTime("yyyyMMdd");
	}
	/**
	 * 获得当前时间 yyyy-MM-dd
	 * @return
	 */
	public static String getFullDate_(){
		return getCurrentDateTime("yyyy-MM-dd");
	}
	/**
	 * 根据格式返回当前日期时间
	 * @param format
	 * @return
	 */
	public static String getCurrentDateTime(String format){
		SimpleDateFormat df=new SimpleDateFormat(format);
		return df.format(new Date());
	}
	/**
	 * 返回星期
	 * @return
	 */
	public static String getWeekay() {
		String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_WEEK);
		String week_day = dayNames[day - 1];
		return week_day;
	}
	public static String getDateDay() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
		return df.format(new Date());
	}
	
	public static String dateFormat(Date date,String format){
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
	/**
	 * 返回YYYY-MM-dd
	 * @param date
	 * @return
	 */
	public static String getStrDate(String date){
		String y=date.substring(0,4);
		String m=date.substring(4,6);
		String d=date.substring(6,8);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append("-").append(m).append("-").append(d);
		return newDate.toString();
	}
	/**
	 * 返回YYYY-MM-dd
	 * @param date
	 * @return
	 */
	public static String getStrDateTime(String date){
		String y=date.substring(0,4);
		String m=date.substring(4,6);
		String d=date.substring(6,8);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append("-").append(m).append("-").append(d).append(" ");
		String h=date.substring(8,10);
		String mm=date.substring(10,12);
		String s=date.substring(12,14);
		newDate.append(h).append(":").append(mm).append(":").append(s);
		return newDate.toString();
	}
	/**
	 * 返回HH:mm:ss
	 * @param date
	 * @return
	 */
	public static String getStrtime(String date){
		String y=date.substring(0,2);
		String m=date.substring(2,4);
		String d=date.substring(4,6);
		StringBuffer newDate=new StringBuffer();
		newDate.append(y).append(":").append(m).append(":").append(d);
		return newDate.toString();
	}
	/**
	 * 判断是否为现在日期
	 * @param date
	 * @return
	 */
	public static boolean isCurrent(String date){
		boolean flag = true;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			Date dt = df.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dt);
			Calendar currentDate = Calendar.getInstance();
			if(currentDate.get(Calendar.YEAR)!=cal.get(Calendar.YEAR)
			  ||currentDate.get(Calendar.MONTH)!=cal.get(Calendar.MONTH)
			  ||currentDate.get(Calendar.DATE)!=cal.get(Calendar.DATE)){
				flag = false;
			}
		} catch (ParseException e) {
		}
		return flag;
	}
	
	public static Calendar stringToCalendar(String date){
		Calendar cal = Calendar.getInstance();
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
			Date dt = df.parse(date);
			cal.setTime(dt);
		} catch (ParseException e) {
		}
		return cal;
	}
	
	public static String calendarToString(Calendar cal){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 获取完整的时间日期
	 * @return
	 */
	public static String getFullTime() {
		return getCurrentDateTime("yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * 获取完整的时间日期
	 * @return
	 */
	public static String getYM() {
		return getCurrentDateTime("yyyy-MM");
	}
	
	/**
	 * 获取完整的时间日期

	 * @return
	 */
	public static String getYearM() {
		return getCurrentDateTime("yyyyMM");
	}
	
	/**
	 * 获取完整的时间日期时间yyyyMMddHHmmss
	 * @return
	 */
	public static String getFullString(){
		return getCurrentDateTime("yyyyMMddHHmmss");
	}
	
	/**
	 * 
	 * 根据字符串格式化为时间
	 * @param format
	 * @return
	 */
	public static Date getDateFormat(String format){
		if(format==null||"".equals(format)){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
		}else{
			return new Date();
		}
	}

	// 测试方法
	public static void main(String[] args) {
		Calendar cal = stringToCalendar("20101231");
		cal.add(Calendar.DATE, 1);
		System.out.println(calendarToString(cal));
	}
	
	public static List getAllDate(String start, String end) throws Exception {
		List list = new ArrayList();
		Date dateTemp = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(start);
		Date dateTemp2 = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(end);
		Calendar calendarTemp = Calendar.getInstance();
		calendarTemp.setTime(dateTemp);
		while (calendarTemp.getTime().getTime()<= dateTemp2.getTime()) {
		    list.add(new SimpleDateFormat("yyyy-MM-dd").format(calendarTemp.getTime()));
		    calendarTemp.add(Calendar.DAY_OF_YEAR, 1);
		}
		return list;
    }
}
